package com.ellecity06.debugtools.ui.request_details

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.ellecity06.debugtools.db.dao.RequestDao
import com.ellecity06.debugtools.db.entity.RequestEntity
import com.ellecity06.debugtools.model.ItemDetailModel
import com.ellecity06.debugtools.utils.PROMPT
import com.ellecity06.debugtools.utils.toJsonStr
import kotlinx.coroutines.withContext

/**
 * @author ellecity06
 * @time 2023/4/21 15:24
 * @des
 */
internal class DebugRequestDetailSource(private val dao: RequestDao, private val id: Int) : PagingSource<Int, ItemDetailModel<RequestEntity>>() {
    override fun getRefreshKey(state: PagingState<Int, ItemDetailModel<RequestEntity>>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ItemDetailModel<RequestEntity>> {

        val it = dao.findById(id) ?: return LoadResult.Error(NullPointerException())
        val list = mutableListOf<ItemDetailModel<RequestEntity>>()
        try {
            val requestBodyJson =
                when {
                    it.requestBody == null -> ""
                    it.requestBody!!.toByteArray().size < 100 * 1024 -> it.requestBody.toJsonStr()
                    else -> PROMPT
                }
            val responseBodyJson =
                when {
                    it.responseBody == null -> ""
                    it.responseBody!!.toByteArray().size < 100 * 1024 -> it.responseBody.toJsonStr()
                    else -> PROMPT
                }
            val decryptRequestBodyJson =
                when {
                    it.decryptRequestBody == null -> ""
                    it.decryptRequestBody!!.toByteArray().size < 100 * 1024 -> it.decryptRequestBody.toJsonStr()
                    else -> PROMPT
                }
            val decryptResponseBodyJson =
                when {
                    it.decryptResponseBody == null -> ""
                    it.decryptResponseBody!!.toByteArray().size < 100 * 1024 -> it.decryptResponseBody.toJsonStr()
                    else -> PROMPT
                }
            list.add(ItemDetailModel(title = "", content = "", searchContent = null, data = it))
            if (it.requestHeader != null && it.requestHeader!!.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Request Header", content = it.requestHeader, searchContent = null, data = it))
            }

            if (decryptRequestBodyJson.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Decrypt Request Body", content = decryptRequestBodyJson, searchContent = null, data = it))
            }
            if (requestBodyJson.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Request Body", content = requestBodyJson, searchContent = null, data = it))
            }
            if (it.responseHeader != null && it.responseHeader!!.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Response Header", content = it.responseHeader, searchContent = null, data = it))
            }
            if (responseBodyJson.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Response Body", content = responseBodyJson, searchContent = null, data = it))
            }
            if (decryptResponseBodyJson.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Decrypt Response Body", content = decryptResponseBodyJson, searchContent = null, data = it))
            }
            if (it.errorMessage != null && it.errorMessage!!.isNotEmpty()) {
                list.add(ItemDetailModel(title = "Error Message", content = it.errorMessage, searchContent = null, data = it))
            }
            return LoadResult.Page(
                data = list,
                prevKey = null,
                nextKey = null
            )
        } catch (e: Exception) {
            return LoadResult.Error(e)
        }

    }
}